package com.example.hzq.food.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.adapter.Buydetailadapter;
import com.example.hzq.food.bean.BuyBean;
import com.example.hzq.food.bean.Success;
import com.example.hzq.food.model.Model;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;

/**
 * Created by hzq on 2017/6/28.
 */

public class Buydetail extends AppCompatActivity implements View.OnClickListener{
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private Buydetailadapter buyadapter;
    private BuyBean bean;
    private TextView readypaid;
    private View buydetailcar;
    private Button paidbutton;
    private ImageButton buydetailback;
    private TextView foodnum,foodsum;
    public static boolean paidflag;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buydetail);
        recyclerView = (RecyclerView)findViewById(R.id.foodsreadybuy);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        bean=(BuyBean) getIntent().getSerializableExtra("bean");
        buyadapter=new Buydetailadapter(this,bean.getFood(),bean.getCount());
        recyclerView.setAdapter(buyadapter);
        readypaid=(TextView)findViewById(R.id.readypaid);
        readypaid.setText("待支付"+bean.getFoodsum()+"");
        buydetailcar=findViewById(R.id.buydetailcar);
        foodnum=(TextView)buydetailcar.findViewById(R.id.foodnum);
        foodsum=(TextView)buydetailcar.findViewById(R.id.foodsum);
        buydetailback=(ImageButton)findViewById(R.id.buyback);
        buydetailback.setOnClickListener(this);
        paidbutton=(Button)buydetailcar.findViewById(R.id.tobuy);
        paidbutton.setOnClickListener(this);
        if (bean.getFoodsum()==0){
            foodnum.setVisibility(View.INVISIBLE);
            foodsum.setText("购物车为空");
            paidbutton.setBackgroundColor(0xFFD6D6D6);
        }
        else {
            foodnum.setVisibility(View.VISIBLE);
            foodnum.setText(bean.getFoodnum()+"");
            foodsum.setText(bean.getFoodsum()+"");
            paidbutton.setBackgroundColor(0xff00ff00);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buyback:
                finish();
                break;
            case R.id.tobuy:
                Model model=new Model();
                Call<Success>call;
              for (int i=0;i<bean.getFood().size();i++){
                  if (i==bean.getFood().size()-1){
                      call=model.insertOrder(Integer.valueOf(userid),bean.getFood().get(i).getFood_id(),bean.getCount().get(i),bean.getFood().get(i).getPrice()*bean.getCount().get(i),"");
                      call.enqueue(new Callback<Success>() {
                          @Override
                          public void onResponse(Call<Success> call, Response<Success> response) {
                              if(response.isSuccessful()&& response.body() != null){
                                  if(response.body().getSuccess().equals("1"))
                                      Toast.makeText(Buydetail.this,"购买成功",Toast.LENGTH_SHORT).show();
                                   paidflag=true;
                                    finish();
                              }
                          }

                          @Override
                          public void onFailure(Call<Success> call, Throwable t) {
                              Toast.makeText(Buydetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                          }
                      });
                  }
                  else{
                      call=model.insertOrder(Integer.valueOf(userid),bean.getFood().get(i).getFood_id(),bean.getCount().get(i),bean.getFood().get(i).getPrice()*bean.getCount().get(i),"");
                      call.enqueue(new Callback<Success>() {
                          @Override
                          public void onResponse(Call<Success> call, Response<Success> response) {
                              if(response.isSuccessful()&& response.body() != null){
                                  if(response.body().getSuccess().equals("0"))
                                      Toast.makeText(Buydetail.this,"购买失败",Toast.LENGTH_SHORT).show();
                              }
                          }

                          @Override
                          public void onFailure(Call<Success> call, Throwable t) {
                              Toast.makeText(Buydetail.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                          }
                      });
                  }


              }
        }
    }
}
